export default{
	strToDate(str,patternStr){
		if(!patternStr){
			patternStr="yyyy-MM-dd hh:mm:ss";
		}
		var date= new Date(0);
		let index=-1;
		if((index=patternStr.indexOf("yyyy"))>-1){
			var year= str.substr(index,4);
			date.setFullYear(year);
		}
		if((index=patternStr.indexOf("MM"))>-1){
			var month=str.substr(index,2);
			date.setDate(15);
			date.setMonth(month-1);
		}else{
			date.setMonth(0);
		}
		if((index=patternStr.indexOf("dd"))>-1){
			var day=str.substr(index,2);
			date.setDate(day);
		}else{
			date.setDate(1);
		}
		if((index=patternStr.indexOf("hh"))>-1){
			var hours=str.substr(index,2);
			date.setHours(hours);
		}else{
			date.setHours(0);
		}
		if((index=patternStr.indexOf("mm"))>-1){
			var minute=str.substr(index,2);
			date.setMinutes(minute);
		}else{
			date.setMinutes(0);
		}
		if((index=patternStr.indexOf("ss"))>-1){
			var second=str.substr(index,2);
			date.setSeconds(second);
		}else{
			date.setSeconds(0);
		}
		return date;
	},
	
	dateFormatter(d,patternStr) {
		if(!patternStr){
			patternStr="yyyy-MM-dd hh:mm:ss";
		}
		var year = d.getFullYear();
		if(patternStr.indexOf("yyyy")>-1){
			patternStr=patternStr.replace(/yyyy/g,year);
		}
		var month = d.getMonth() + 1;
		if(patternStr.indexOf("MM")>-1){
			if (month < 10) {
				month = '0' + month;
			}
			patternStr=patternStr.replace(/MM/g,month);
		}else if(patternStr.indexOf("M")>-1){
			patternStr=patternStr.replace(/M/g,month);
		}
		var date = d.getDate();
		if(patternStr.indexOf("dd")>-1){
			if (date < 10) {
				date = '0' + date;
			}
			patternStr=patternStr.replace(/dd/g,date);
		}else if(patternStr.indexOf("d")>-1){
			
			patternStr=patternStr.replace(d/g,date);
		}
		var hour = d.getHours();
		if(patternStr.indexOf("hh")>-1){
			if (hour < 10) {
				hour = '0' + hour;
			}
			patternStr=patternStr.replace(/hh/g,hour);
		}else if(patternStr.indexOf("h")>-1){
			patternStr=patternStr.replace(/h/g,hour);
		}
		var minute = d.getMinutes();
		if(patternStr.indexOf("mm")>-1){
			if (minute < 10) {
				minute = '0' + minute;
			}
			patternStr=patternStr.replace(/mm/g,minute);
		}else if(patternStr.indexOf("m")>-1){
			patternStr=patternStr.replace(/m/g,minute);
		}
		var second = d.getSeconds();
		if(patternStr.indexOf("ss")>-1){
			if (second < 10) {
				second = '0' + second;
			}
			patternStr=patternStr.replace(/ss/g,second);
		}else {
			if(patternStr.indexOf("s")>-1){
				patternStr=patternStr.replace(/s/g,second);
			}
		}
		return patternStr;//必须是‘/’格式
	},
	//以右边对象为准
	objectEq(arg0,arg1){
		for(var p in arg1){
			if(arg0[p]!=arg1[p]){
				return false;
			}
		}
		return true;
	},
	delFromArray(){
		if(!arguments[0]||!(arguments[0] instanceof Array)){
			throw 'args[0] must be an Array';
		}
		if(!arguments[1]||!(arguments[1] instanceof Object)){
			throw 'args[1] must be an Object';
		}
		let dataList=arguments[0];
		for(var i=0;i<dataList.length;i++){
			if(this.objectEq(dataList[i],arguments[1])){
				dataList.splice(i,1);
			}
		}
	},
	contain(arr,obj,p){
		if(this.indexOfArray(arr,obj,p)>-1){
			return true;
		}else{
			return false;
		}
	},
	indexOfArray(arr,obj,p){
		if(!arr||arr==null||arr.length==0){
			return -1;
		}
		if(typeof obj==='string'||typeof obj==='number'){
			return arr.indexOf(obj);
		}else {
			let obj2={};
			if(!p){
				throw 'value of p is not correct';
			}
			if(!(p instanceof Array)){
				p=[p];
			}
			this.extend(obj2,obj,p);
			for(var i=0;i<arr.length;i++){
				if(p){
					if(this.objectEq(arr[i],obj2)){
						return i;
					}
				}else{
					if(arr[i]===obj){
						return i;
					}
				}
			}
		}
		return -1;
	},
	mergeDataList(){
		if(!arguments[0]||!(arguments[0] instanceof Array)){
			throw 'args[0] must be an Array';
		}
		if(!arguments[1]||!(arguments[1] instanceof Array)){
			throw 'args[1] must be an Array';
		}
		if(!arguments[2]||!(typeof arguments[2]==='string'||arguments[2] instanceof Array)||arguments[2]===''){
			throw 'args[2] must be a name of property of args[0] and args[1]';
		}
		let newArray=[];
		for(var i=0;i<arguments[0].length;i++){
			newArray.push(arguments[0][i]);
		}
		for(var i=0;i<arguments[1].length;i++){
			if(!this.contain(newArray,arguments[1][i],arguments[2])){
				newArray.push(arguments[1][i]);
			}
		}
		return newArray;
	},
	groupBy(arr,groupKey,opt){
		if(!arguments[0]){
			return arr;
		}
		if(!(arguments[0] instanceof Array)){
			throw 'args[0] must be an Array';
		}
		let groups=[];
		let extra={};
		let optFunctions={
			sum(handler,index,names,data){
				let sum=handler;
				if(index<0){
					index= sum.push({})-1;
				}
				if(typeof names==='string'){
					names=[names];
				}
				for(let i=0;i<names.length;i++){
					let name=names[i];
					if(sum[index][name]){
						sum[index][name]+=parseFloat(this.praseStrEmpty(data[name],0));
					}else{
						sum[index][name]=parseFloat(this.praseStrEmpty(data[name],0));
					}
					sum[index][name]=parseFloat(this.fixed(sum[index][name],2));
				}
				
			}
		}
		let dataList=[];
		let index=-1;
		for(var i=0;i<arr.length;i++){
			let groupVal;
			if(typeof groupKey==='string'){
				groupVal=arr[i][groupKey];
			}else if(typeof groupKey==='function'){
				groupVal=groupKey(arr[i]);
			}
			if((index=this.indexOfArray(groups,groupVal))>-1){
				dataList[index].push(arr[i]);
			}else{
				groups.push(groupVal);
				dataList.push(new Array(arr[i]));
			}
			for(var p in opt){
				if(optFunctions[p]){
					if(!extra.hasOwnProperty(p)){
						extra[p]=[];
					}
					optFunctions[p].call(this,extra[p],index,opt[p],arr[i]);
				}
			}
			
		}
		return this.extend({groups:groups,dataList:dataList},extra);
	},
	fixed(num,n){
		// num= num+"";
		// let index= num.indexOf(".");
		// if(index>0){
		// 	return parseFloat(num.substr(0,index)+num.substr(index,n+1));
		// }else{
		// 	return parseFloat(num);
		// }
		return parseFloat(num).toFixed(n);
		 
	},
	extend(arg0,arg1,arg2){
		if(!arg2){
			for(var p in arg1){
				arg0[p]=arg1[p];
			}
		}else{
			if(!(arg2 instanceof Array)){
				arg2=[arg2];
			}
			for(var i=0;i<arg2.length;i++){
				arg0[arg2[i]]=arg1[arg2[i]];
			}
		}
		
		return arg0;
	},
	extendNotInclude(arg0,arg1,arg2){
		if(!arg2){
			for(var p in arg1){
				arg0[p]=arg1[p];
			}
		}else{
			if(!(arg2 instanceof Array)){
				arg2=[arg2];
			}
			for(var p in arg1){
				if(this.indexOfArray(arg2,p)==-1){
					arg0[p]=arg1[p];
				}

			}
		}
		return arg0;
	},
	computeParkTime(t){
		let s='';
		t=t/1000;
		let d=Math.floor(t/(60*60*24));
		let h=Math.floor( (t%(60*60*24))/(60*60));
		let m=Math.floor( (t%(60*60)/(60)));
		if(d>0){
			s=d+'天';
		}
		if(s!=''||h>0){
		   s=s+h+'小时';
		}
		s= s+m+'分钟';
		return s;
	},
	praseStrEmpty(str,def) {
	    if (this.isEmpty(str)){
	        return typeof def==='undefined'?"":def;
	    }
	    return str;
	},
	isEmpty(str){
		return typeof str === 'undefined' || str === null||str==""||(typeof str==='number'&&isNaN(str))
	},
	strlen(value){
		var length=0;
		for(var i=0;i<value.length;i++){
			if(this.isZHCN(value.charAt(i))){
				length+=2;
			}else{
				length+=1;
			}
		}
		return length;
	},
	isZHCN(s){
		var test=/[\u4E00-\u9FA5]/;
		return test.test(s);
	},
	shortStr(str,n,c){
		if(!n){
			n=7;
		}
		if(!c){
			c='...'
		}
		n=n*2;
		let newStr="";
		let l=0;
		if(typeof str==='string'){
			for(var i=0;i<str.length;i++){
				if(l>n){
					newStr+=c;
					break;
				}
				if(this.isZHCN(str[i])){
					l+=2;newStr+=str[i];
				}else{
					l+=1;newStr+=str[i];
				}
			}
			// str= str.substr(0,n);
			// str+="...";
		}
		return newStr;
	},
	isEmptyObject(e) {
		var t;
		for (t in e)
			return !1;
		return !0
	},
	stringify(){
		let cache=[];
		let jsonStr=JSON.stringify(msg,(key,value)=>{
			if(typeof value==='object'&&value!==null){
			if(cache.indexOf(value)!==-1){
				return;
			}
			cache.push(value);
			}
			return value;
		});
		return jsonStr;
	},
	getRequestParamter(k,url){
		let search;
		if(url&&url.indexOf("?")>-1){
			search=url.substr(url.indexOf("?")+1);
		}else{
			search=window.location.search.substr(1);
		}
		let params= search.split("&");
		for(var i=0;i<params.length;i++){
			let kv= params[i].split("=");
			if(kv.length>1&&kv[0]===k){
				return kv[1];
			}
		}
		return "";
	}
}